#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/hash.h"
#include "../include/addfile.h"
#include "../include/vfs_errorcodes.h"

struct fileDescriptor * copyfile1(char sourceLocation[],char destinationLocation[])
{
	FILE *fp,*ft;
	struct header h1;
	struct Block b1;
	struct freeList fl1;
	struct fileDescriptor *fd1,*fd2,*fd3;
	int i,j,n,size1,size2,blockno,freelistno,freefileDescriptor,index,count;
	freefileDescriptor=0;
	char datas[1024],newData[1024],fname[30],fileloc[100],str[100],newstr[30];
	//printf("in copy\n");
	//scanf("%s",diskfilepath);
	//printf("copyloc:%s\nname:%s\n",destinationLocation,sourceLocation);
	strcpy(fileloc,sourceLocation);
//--------------------------------------------------------------------------------------------------------
//splitting location path into location and file name
	
	char * result =strtok(sourceLocation,"/");
	while(result!=NULL)
	{
		strcpy(fname,result);
		result=strtok(NULL,"/");			
		
	}
	//printf("FNAME:%s\n",fname);


//---------------------------------------------------------------------------------------------------------
//searching file in the hash table

	struct linkedlist *ptr;
	index=hash(fname);
	ptr=hashtable[index];
	char type[10];
		
	while(ptr!=NULL)
	{
		//printf("in while loop\n");
		if(strcmp(ptr->fd->locationPath,fileloc)==0)
		{
			//strcpy(fileloc,ptr->fd->locationPath);
			//printf("File name hash:%s\n",ptr->fName);
			//printf("File name fd:%s\n",ptr->fd->fileName);
			strcpy(type,(ptr->fd->fileType)); 
			//printf("file found");
			//printf("file type in copy file %s \n",ptr->fd->fileType);	
			//printf("File location:%s\n",ptr->fd->locationPath);
			//printf("File location:%d\n",ptr->fd->blockNumber);
			//printf("File location:%d\n",ptr->fd->fileSize);
			//printf("%s\n",fileloc);
			break;
		}
		else
		ptr=ptr->nextNode;
	}
//---------------------------------------------------------------------------------------------------------
//open the file system and seek to the corresponding block of the file
	if(ptr==NULL)
	{
		printf("copyfile_FAILURE ");
		printf(ERR_VFS_COPYFILE_01);
		printf("\n");
		return NULL;

	}


	fp=fopen(flsname,"rb+");
	fread(&h1,sizeof(struct header),1,fp);
	n=h1.maxfd;
	size1=sizeof(struct header)+n*sizeof(struct freeList);
	size2=(size1+(n*(sizeof(struct fileDescriptor)))+(ptr->fd->blockNumber*(sizeof(struct Block))));
	fseek(fp,size2,SEEK_SET);
		fread(&b1,sizeof(struct Block),1,fp);
			//printf("%c ",b1.isfull);
			if(b1.isfull=='1')
			{
				
				//printf("\nFile contents : %s \n",b1.text);
				strcpy(newData,b1.text);
				
			}
			//printf("data=%s\n",newData);
	fclose(fp);
//---------------------------------------------------------------------------------------------------------
//splitting path into path location of file and file name
	strcpy(str,destinationLocation);
	i=0;
	count=0;
	while(str[i]!='\0')
	{
	if(str[i]=='/')
	count=i;
	i++;
	}
	count++;
	j=0;
	while(str[count]!='\0')
	{
		newstr[j]=str[count];
	count++;
	j++;
	}
	newstr[j]='\0';
	//printf("new:%s\n",newstr);

	fd3=searchBstfd(destinationLocation);
	if(fd3!=NULL)
	{
	
		//printf("\n");
		//printf("ftype:%s\n",fd3->fileType);
	
		if(strcmp(fd3->fileType,"dir")==0)
		{
		printf("copyfile_FAILURE ");
		printf(ERR_VFS_COPYFILE_03);
		printf("\n");
		return NULL;
		}
		else
		{
			ft=fopen(flsname,"rb+");
			if(ft==NULL)
			{
				printf("copyfile_FAILURE ");
				printf(ERR_VFS_ADDFILE_05);
				printf("\n");
				return NULL;

			}
			//printf("File overwritten\n");
			fread(&h1,sizeof(struct header),1,ft);
			n=h1.maxfd;
			size1=sizeof(struct header)+n*sizeof(struct freeList);
			size2=(size1+(n*(sizeof(struct fileDescriptor)))+(fd3->blockNumber*(sizeof(struct Block))));
			fseek(ft,size2,SEEK_SET);
			strcpy(b1.text,datas);
			b1.isfull='1';
			fwrite(&b1,sizeof(struct Block),1, ft );
			//printf("\nFile written to disk\n");				
			return NULL;			
		}					



	}
	else
	{
	//printf("not found\n");
	}

		

//add the file to block and get the FD back

	fd2=addFDFile1(destinationLocation,newstr,type,newData,fd3->fileSize);
	//printf("newstr:%s\n",newstr);
	//printf("Fd name:%s\n",fd2->fileName);
	//printf("Fd loc 	:%s\n",fd2->locationPath);
	//printf("Fd type :%s\n",fd2->fileType);
	//printf("Fd type	:%d\n",fd2->blockNumber);
	//printf("\nin file : %p \n",fd2);
	return fd2;
}

